package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.DisposeExport;
import com.css.fxfzaqbz.modules.data.entity.DisposeManager;
import com.css.fxfzaqbz.modules.data.repository.DisposeExportRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DisposeExportRepositoryImpl implements DisposeExportRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<DisposeExport> saveDisposeExport(List<DisposeExport> disposeExports) {
        for (DisposeExport disposeExport : disposeExports) {
            String sql = "insert into sys_dispose_export values (:exportId,:activeId,:activeName,:dmId,:delFlag,:createUser,:createTime,:updateUser,:updateTime)";
            SqlParameterSource source = new BeanPropertySqlParameterSource(disposeExport);
            KeyHolder keyHolder = new GeneratedKeyHolder();
            namedParameterJdbcTemplate.update(sql, source, keyHolder);
        }
        return disposeExports;
    }

    @Override
    public List<DisposeExport> getDisposeExport(String id) {
        String sql = "select * from sys_dispose_export where active_id = '" + id + "'  and  del_flag= '" + YNEnum.Y + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisposeExport.class));
    }

    @Override
    public void delDisposeExport(List<String> ids, Timestamp timestamp) {
        String sql = "update sys_dispose_export set del_flag = '" + YNEnum.N + "' , update_time = '" + timestamp + "' " + " where export_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }
}
